package com.zhihuishu.innovationcourse;


import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withParent;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.matcher.BoundedMatcher;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;

import org.hamcrest.Description;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityScenarioRule<MainActivity> mActivityScenarioRule =
            new ActivityScenarioRule<>(MainActivity.class);

    @Test
    public void mainActivityTest() {
        ViewInteraction imageView = onView(
                allOf(withId(R.id.teacher_small_imageView),
                        withParent(withParent(withId(R.id.teacher_listView))),
                        isDisplayed()));

//        imageView.check(matches(isDrawable()));

        ViewInteraction textView = onView(
                allOf(withId(R.id.teacher_name_textView), withText("张海霞"),
                        withParent(withParent(withId(R.id.teacher_listView))),
                        isDisplayed()));
        textView.check(matches(withText("张海霞")));

        ViewInteraction textView2 = onView(
                allOf(withId(R.id.teacher_name_textView), withText("张海霞"),
                        withParent(withParent(withId(R.id.teacher_listView))),
                        isDisplayed()));
        textView2.check(matches(withText("张海霞")));
    }

    private BoundedMatcher<View, ImageView> isDrawable() {
        return new BoundedMatcher<View, ImageView>(ImageView.class) {
            @Override
            protected boolean matchesSafely(ImageView item) {

                int integer = (int) item.getTag();
//                integer = integer == null ? 0 : integer;
                return integer == R.drawable.zhx;
            }

            @Override
            public void describeTo(Description description) {
//                description.appendText("是否有drawable");
            }


        };
    }
}
